package com.qianfeng.gl4study.youshare.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.acitivity.store.CoreProductInfoActivity;
import com.qianfeng.gl4study.youshare.adapters.StoreCoreAdapter;
import com.qianfeng.gl4study.youshare.model.Product;
import com.qianfeng.gl4study.youshare.tasks.ProductListTask;
import com.qianfeng.gl4study.youshare.tasks.TaskProcessor;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/5
 * Email:pdsfgl@live.com
 */
public class StoreCoreFragment extends Fragment implements TaskProcessor, AdapterView.OnItemClickListener {

	private boolean hasFirstLoad = false;//用于标记是否已经加载过一次数据，即判断视图是否创建完成
	private LinkedList<Product> products;
	private StoreCoreAdapter adapter;

	public StoreCoreFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View ret = inflater.inflate(R.layout.fragment_store_core, container, false);
		ListView listView = (ListView) ret.findViewById(R.id.store_core_product_list);
		if(listView!=null) {
			products = new LinkedList<Product>();
			adapter = new StoreCoreAdapter(getActivity(), products);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(this);
		}
		if(!hasFirstLoad){
			ProductListTask task = new ProductListTask(this);
			task.execute("1","10");
		}
		return ret;
	}

	/**
	 * 点击核心商品列表，跳转到核心商品详细信息
	 * @param adapterView
	 * @param view
	 * @param i
	 * @param l
	 */
	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
		Product product = products.get(i);
		Intent intent = new Intent(getActivity(), CoreProductInfoActivity.class);
		intent.putExtra("product",product);
		startActivity(intent);
		getActivity().finish();
	}
	/**
	 * 异步任务更新UI的回调方法
	 * @param result
	 * @param flag
	 */
	@Override
	public void processResult(JSONObject result, int flag) {
		//显示核心商品列表
		hasFirstLoad = true;
		try {
			int code = result.getInt("code");
			//code为1，代表返回数据正确，msg是一个对象，若code不是1，代表数据请求失败，msg返回字符串
			if(code == 1){
				//数据正确，需要解析
				JSONObject msg = result.getJSONObject("msg");
				JSONObject data = msg.getJSONObject("data");
				Iterator iterator = data.keys();
				List<Product> productList = new LinkedList<Product>();
				while (iterator.hasNext()){
					Object key = iterator.next();
					JSONObject object = data.getJSONObject((String) key);
					Product product = new Product();
					product.parseJSON(object);
					productList.add(product);
				}
				//如果刷新数据，那么products需要清空，然后使用productList里面的内容添加到products，即：重新加载
				//如果实现加载更多，那么就不需清空，可以直接添加
				products.addAll(productList);
				adapter.notifyDataSetChanged();

			}else {
				//数据请求失败
				String msg = result.getString("msg");
				Toast.makeText(getActivity(), msg,Toast.LENGTH_LONG).show();
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}


}